package vista;

import java.awt.Point;
import java.awt.geom.Rectangle2D;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.plot.XYPlot;
import org.jfree.ui.RectangleEdge;

public class MouseEventListenerApplet implements ChartMouseListener{
	
	
	private ChartPanel chartPanel;
	private SimuladorGUIApplet simuladorGUI;

	public MouseEventListenerApplet(SimuladorGUIApplet simuladorGui, ChartPanel chartpanel){
		this.simuladorGUI = simuladorGui;
		this.chartPanel = chartpanel;
	}
	
	
	@Override
	public void chartMouseClicked(ChartMouseEvent arg0) {

		Point point = arg0.getTrigger().getPoint(); 
		//add a new point to the model editor series 
		int x = point.x; 
		int y = point.y; 
		
		Rectangle2D dataArea = chartPanel.getScreenDataArea();
		float x0 = (float) chartPanel.getChart().getXYPlot().getRangeAxis().java2DToValue(y, dataArea, RectangleEdge.LEFT); 
		float t0 = (float) chartPanel.getChart().getXYPlot().getDomainAxis().java2DToValue(x, dataArea, RectangleEdge.BOTTOM);
		
		simuladorGUI.nuevoPuntoInicial(t0, x0);
	}

	@Override
	public void chartMouseMoved(ChartMouseEvent arg0) {
		Double chartX;
		Double chartT;	
		
		XYPlot plot = (XYPlot) chartPanel.getChart().getPlot(); // your plot
		chartX = plot.getRangeAxis().java2DToValue(chartPanel.translateScreenToJava2D(arg0.getTrigger().getPoint()).getY(), chartPanel.getScreenDataArea(), plot.getRangeAxisEdge());
		chartT = plot.getDomainAxis().java2DToValue(chartPanel.translateScreenToJava2D(arg0.getTrigger().getPoint()).getX(), chartPanel.getScreenDataArea(), plot.getDomainAxisEdge());

		simuladorGUI.actualizarPosicionCursor(chartX, chartT);
		
	}

}
